﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FinPlusWPFControls
{
    //Note Prime use edit template columns use primative controls for data template
    public partial class FinPlusValueChangeTextBox : FinPlusControl
    {
        public override string Id { get { return GetValue(IdProperty).ToString(); } set { } }
        public override string[] TestStates { get { return new string[] { "TestString", "ViewString", "IsValid" }; } }
        public override string TestString { get { return string.Format("{0}|{1}", IsEnabled, Text); } }

        public string ViewString { get { return string.Format("{0}|{1}", textBlock.IsEnabled, textBlock.Text);} }
        public string FunctionName { get { return GetValue(FunctionNameProperty).ToString(); } set { } }
        public string Text { get { return GetValue(TextProperty).ToString(); } set { SetValue(TextProperty, value); } }
        public int Status { get { return (int)GetValue(StatusProperty); } set { SetValue(StatusProperty, value); } }
        public string StringFormat { get { return GetValue(StringFormatProperty).ToString(); } }

        public static readonly DependencyProperty IdProperty = DependencyProperty.Register(Propertys.Id.ToString(), typeof(string), typeof(FinPlusValueChangeTextBox));
        public static readonly DependencyProperty FunctionNameProperty = DependencyProperty.Register(Propertys.FunctionName.ToString(), typeof(string), typeof(FinPlusValueChangeTextBox));
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(Propertys.Text.ToString(), typeof(string), typeof(FinPlusValueChangeTextBox), new UIPropertyMetadata(Callback));
        public static readonly DependencyProperty StatusProperty = DependencyProperty.Register(Propertys.Status.ToString(), typeof(int), typeof(FinPlusValueChangeTextBox));
        public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register(Propertys.StringFormat.ToString(), typeof(string), typeof(FinPlusValueChangeTextBox));
      
        private enum Propertys { Id, FunctionName, Text, IsEnabled, Status, StringFormat, InputMask }
        private double _oldValue = double.MinValue;

        //construct
        public FinPlusValueChangeTextBox()
        {
            InitializeComponent();
            IsValid = true;

            textBlock.Cursor = ((TextBlock)this.Resources["CPoint"]).Cursor;
        }

        //private
        private void SetControl(DependencyPropertyChangedEventArgs a)
        {
            //format
            double newValue;
            if (double.TryParse(a.NewValue.ToString(), out newValue))
            {
                textBlock.Foreground = newValue < 0 ? Brushes.Red : Brushes.Black;
                Status = _oldValue == double.MinValue ? 0 : newValue > _oldValue ? 1 : -1;
                _oldValue = newValue;
            }
            else
                Status = 0;
        }

        //event
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValueChangeTextBox)d).SetControl(a); }
    }   
}
